package com.example.myapplication2;

import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class CheckBoxActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
        CheckBox viewById = findViewById(R.id.checkBox1);
        //代码设置选中
        viewById.setChecked(true);
        CheckBox viewById2 = findViewById(R.id.checkBox2);
        viewById2.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch (compoundButton.getId()){
            case R.id.checkBox2:
                //设置文本
                compoundButton.setText("目前选择状态为"+b);
                break;
        }
    }
}